//
//  ChangePasswordVC.m
//  KuWoCan
//
//  Created by hwd on 17/4/6.
//  Copyright © 2017年 hwd. All rights reserved.
//

#import "ChangePasswordVC.h"
#import "NWLoginRequest.h"
#import "PasswordCell.h"

#define OldPassword   @"oldPassword"

#define NewPassword   @"newPassword"

#define CheckPassword   @"checkPassword"

@interface ChangePasswordVC ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) NSMutableDictionary *paraDic;

@end

@implementation ChangePasswordVC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setTitle:@"修改密码"];
    [self paraDic];
    [self setUpUI];
}

- (NSMutableDictionary *)paraDic
{
    if (!_paraDic)
    {
        _paraDic = [NSMutableDictionary dictionary];
    }
    return _paraDic;
}

- (void)btnClick
{
    [self.view endEditing:YES];
    BOOL isOk = YES;
    if (_paraDic.count >= 3)
    {
        NSString *oldPw = _paraDic[OldPassword];
        NSString *newPw = _paraDic[NewPassword];
        NSString *checkPw = _paraDic[CheckPassword];
        NSArray *tempArr = @[oldPw,newPw,checkPw];
        for (NSString *str in tempArr)
        {
            if ([CommonTool isEmpty:str])
            {
                isOk = NO;
            }
        }
        
        if (isOk)
        {
            if ([newPw isEqualToString:checkPw])
            {
                __weak typeof(self) weakSelf = self;
                UserModel *user = [UserModel getUser];
                NWLoginRequest *request = [[NWLoginRequest alloc] init];
                [request startRequestChangePasswordWithId:[Singleton shareInstance].userId password:oldPw newPassword:newPw loginName:user.loginName];
                [request completion:^(id result, NSString *message, id extension, BOOL succ) {
                    if (succ)
                    {
                        [SVProgressHUD showSuccessWithStatus:@"密码修改成功"];
                        [weakSelf.navigationController popViewControllerAnimated:YES];
                    }
                    else
                    {
                        [SVProgressHUD showErrorWithStatus:message];
                    }
                }];
            }
            else
            {
                [SVProgressHUD showErrorWithStatus:@"请确认两次输入密码是否一致"];
            }
        }
        else
        {
            [SVProgressHUD showErrorWithStatus:@"请输入完整信息"];
        }
    }
    else
    {
        [SVProgressHUD showErrorWithStatus:@"请输入完整信息"];
    }
}


- (void)setUpUI
{
    self.view.backgroundColor = KwBgColor;
    
    TPKeyboardAvoidingTableView *tabView = [[TPKeyboardAvoidingTableView alloc] initWithFrame:CGRectMake(0, 0, 0, 0) style:UITableViewStylePlain];
    tabView.delegate = self;
    tabView.dataSource = self;
    tabView.rowHeight = UITableViewAutomaticDimension;
    tabView.estimatedRowHeight = 40.0;
    tabView.backgroundColor = [UIColor whiteColor];
    tabView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:tabView];
    [tabView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.bottom.equalTo(@0);
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 3;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 80;
}

- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [UIView new];
    UIButton *confirmBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    [confirmBtn setBackgroundColor:KwBlueColor];
    [confirmBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [confirmBtn setTitle:@"确认" forState:UIControlStateNormal];
    confirmBtn.titleLabel.font = KwFont(16.0);
    [confirmBtn addTarget:self action:@selector(btnClick) forControlEvents:UIControlEventTouchUpInside];
    confirmBtn.layer.masksToBounds = YES;
    confirmBtn.layer.cornerRadius = 5.0;
    [footerView addSubview:confirmBtn];
    [confirmBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@40);
        make.top.equalTo(@20);
        make.left.equalTo(@10);
        make.right.equalTo(@-10);
    }];
    return footerView;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSArray *titleArr = @[@"原密码",@"新密码",@"确认密码"];
    static NSString *cellID = @"LabTextFieldCell";
    PasswordCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell)
    {
        cell = [[PasswordCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
//    cell.leftLab.text = titleArr[indexPath.row];
    [cell configCell:titleArr[indexPath.row]];
    cell.block = ^(NSString *str){
        if (indexPath.row == 0)
        {
            [_paraDic setValue:str forKey:OldPassword];
        }
        else if (indexPath.row == 1)
        {
            [_paraDic setValue:str forKey:NewPassword];
        }
        else
        {
            [_paraDic setValue:str forKey:CheckPassword];
        }
    };
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
